﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityPlayerUtil.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-15
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityPlayerUtil
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityPlayerUtil.h"

#include "NFCityMapUtil.h"
#include "NFCityPlayer.h"
#include "NFCitySession.h"

NFCityPlayer* NFCityPlayerUtil::GetPlayerByRoleID(uint64_t roleID)
{
    return NFCityPlayer::GetObjByHashKey(roleID);
}

NFCityPlayer* NFCityPlayerUtil::CreatePlayer(uint64_t roleID)
{
    NFCityPlayer* pPlayer = NFCityPlayer::CreateObjByHashKey(roleID);
    CHECK_NULL_RE_NULL(roleID, pPlayer, "create player failed:{}", roleID);

    LOG_INFO(roleID, "Create player:{}", roleID);
    pPlayer->SetRoleID(roleID);

    return pPlayer;
}

int NFCityPlayerUtil::DeletePlayer(NFCityPlayer* pPlayer)
{
    CHECK_NULL(0, pPlayer);
    LOG_INFO(pPlayer->GetRoleID(), "Delete player:{}, globalId", pPlayer->GetRoleID(), pPlayer->GetGlobalId());
    pPlayer->Fini();
    NFCityPlayer::DestroyObj(pPlayer);
    return 0;
}

int NFCityPlayerUtil::RemovePlayer(NFCityPlayer* pPlayer)
{
    CHECK_NULL(0, pPlayer);

    LOGSVR_TRACE("CWorldProcessor RemovePlayer");

    auto pSession = pPlayer->GetClientSession();
    if (pSession)
    {
        //主动关连接
        pSession->CloseConnect();
    }

    NFCityMapUtil::LeaveMap(pPlayer);

    //直接回收对象
    DeletePlayer(pPlayer);

    return 0;
}

int NFCityPlayerUtil::OfflinePlayer(NFCityPlayer* pPlayer)
{
    CHECK_NULL(0, pPlayer);
    LOG_INFO(pPlayer->GetRoleID(), "Logout player:{}", pPlayer->GetRoleID());
    pPlayer->BindClientSession(nullptr);
    pPlayer->SetPlayerStatus(CACHE_STATUS_OFFLINE);
    pPlayer->SetLastDiconnectTime(NF_ADJUST_TIMENOW());
    return 0;
}

bool NFCityPlayerUtil::IsPlayerInTeam(NFCityPlayer* pPlayer)
{
    if (pPlayer)
    {
        return pPlayer->GetTeamExt()->GetTeamMemberNum() > 0;
    }
    return false;
}

bool NFCityPlayerUtil::IsPlayerInSameTeam(NFCityPlayer* pPlayer, NFCityPlayer* pTargetPlayer)
{
    if (pPlayer && pTargetPlayer)
    {
        return pPlayer->GetTeamExt()->IsInTeam(pTargetPlayer->GetRoleID());
    }

    return false;
}

bool NFCityPlayerUtil::IsPlayerTeamLeader(NFCityPlayer* pPlayer)
{
    if (pPlayer)
    {
        return pPlayer->GetTeamExt()->IsTeamLeader(pPlayer->GetRoleID());
    }

    return false;
}
